package cn.wei.reallyEnjoy.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhy.http.okhttp.OkHttpUtils;

import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;

import cn.wei.reallyEnjoy.R;
import cn.wei.reallyEnjoy.user.bean.User;
import cn.wei.reallyEnjoy.utils.Constants;
import okhttp3.Call;

public class LoginActivity extends AppCompatActivity {
    @BindView(R.id.ib_login_back)
    ImageButton ib_login_back;

    @BindView(R.id.tv_no_account)
    TextView tv_no_account;

    @BindView(R.id.btn_login)
    Button btn_login;

    @BindView(R.id.et_account)
    EditText et_account;

    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.cb_remember_account_password)
    CheckBox cb_remember_account_password;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //去掉窗口标题
        getSupportActionBar().hide();
        //ButterKnife和当前Activity绑定
        ButterKnife.bind(this);
        readLoginInfo();
        initListener();


    }

    private void readLoginInfo() {
        SharedPreferences sp = getSharedPreferences("login",Context.MODE_PRIVATE);
        boolean rememberAccountPassword = sp.getBoolean("rememberAccountPassword",false);
        if (rememberAccountPassword){
            //回显账号密码，选中记住
            String mobile = sp.getString("mobile","");
            String password = sp.getString("password","");
            et_account.setText(mobile);
            et_password.setText(password);
            cb_remember_account_password.setChecked(true);

        }else{
            cb_remember_account_password.setChecked(false);

        }

    }

    //设置监听
    public void initListener() {
        Log.e(null,"Login页面");
        ib_login_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
               startActivity(intent);
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = Constants.LOGIN_URL;

                String account = et_account.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                SharedPreferences login = LoginActivity.this.getSharedPreferences("login", Context.MODE_PRIVATE);
                if (cb_remember_account_password.isChecked()){

                    //保存登录信息
                    SharedPreferences.Editor editor = login.edit();
                    editor.putString("mobile",account);
                    editor.putString("password",password);
                    editor.putBoolean("rememberAccountPassword",true);
                    editor.commit();

                }else{
                    login.edit().clear().commit();

                }

                System.out.println(account);
                System.out.println(password);
                if(!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)){
                    OkHttpUtils
                            .post()
                            .url(url)
                            .addParams("mobile", account)
                            .addParams("password", password)
                            .build()
                            .execute(new StringCallback() {
                                         @Override
                                         public void onError(Call call, Exception e, int id) {
                                             Toast.makeText(LoginActivity.this, "网络错误，登录失败", Toast.LENGTH_SHORT).show();
                                         }

                                         @Override
                                         public void onResponse(String response, int id) {
                                             //json解析
                                             JSONObject jsonObject = JSON.parseObject(response);
                                             String code = (String) jsonObject.get("code");
                                             if (!TextUtils.equals(code,"0")){
                                                 String message = (String) jsonObject.get("msg");
                                                 Toast.makeText(LoginActivity.this,message, Toast.LENGTH_SHORT).show();
                                             }else{
                                                 //解析json数据，生成User对象
                                                 //获取data节点
                                                 String data =String.valueOf(jsonObject.get("data"));
                                                 User user = JSON.parseObject(data, User.class);

                                                 //保存用户信息
                                                 SharedPreferences sp = LoginActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
                                                 SharedPreferences.Editor editor = sp.edit();
                                                 editor.putString("nickName",user.getNickName());
                                                 editor.putString("headImg",user.getHeadImg());
                                                 editor.putString("mobile",user.getMobile());
                                                 editor.putString("sex",user.getSex());
                                                 editor.putString("id",user.getId());
                                                 editor.commit();//提交保存

                                                 //从新加载界面
//
                                                 startActivity(new Intent(new Intent(LoginActivity.this, MainActivity.class)));
                                             }

                                         }
                                     }
                            );

                }else{
                    Toast.makeText(LoginActivity.this, "账号或面错误", Toast.LENGTH_SHORT).show();
                }

            }
        });
        tv_no_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });
    }

}